package com.sjy.configuration;

/**
 * @author 苏健尧
 * appearance  非常帅
 */

import com.sjy.websocket.MyWebSocketHandler;
import com.sjy.websocket.constant.WsConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.*;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * @author GXM
 * @author black
 * @version 1.0.0
 * @Description TODO
 * @createTime 2023年07月06日
 * package org.spring.boot.websocket;
 * <p>
 * import org.springframework.context.annotation.Bean;
 * import org.springframework.context.annotation.Configuration;
 * import org.springframework.web.socket.WebSocketHandler;
 * import org.springframework.web.socket.config.annotation.EnableWebSocket;
 * import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
 * import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistration;
 * import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
 * <p>
 * /**
 * WebSocket 配置类
 */
@EnableWebSocketMessageBroker
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    @Bean
    public ServerEndpointExporter serverEndpoint() {
        return new ServerEndpointExporter();
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 注册 Stomp的端点（Endpoint），并且映射指定的url
        registry.addEndpoint(WsConstant.PATH_PREFIX)
                .setAllowedOrigins("*"); // 添加允许跨域访问
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // 启动广播模式代理，只有符合的的路径才发送消息
        registry.enableSimpleBroker("/topic");
    }
}




